package com.javatian.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javatian.dao.RichtextInfoCommentDao;
import com.javatian.entity.Account;
import com.javatian.entity.RichtextInfoComment;
import com.javatian.entity.GoodsInfo;
import com.javatian.service.*;
import com.javatian.vo.RichtextInfoCommentVo;
import exception.CustomException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 车辆评价信息对外提供的服务实现类
 */
@Service("richtextInfoCommentService")
public class RichtextInfoCommentServiceImpl implements RichtextInfoCommentService {
    @Resource
    private RichtextInfoCommentDao richtextInfoCommentDao;


    /**
     * 增加一条记录
     */
    @Override
    public RichtextInfoComment add(RichtextInfoComment commentInfo,Account account) {
        if (account == null){
            throw new CustomException("1001","请先登录！");
        }
        commentInfo.setCreateTime(DateUtil.formatDateTime(new Date()));
        String content = commentInfo.getContent();
        if (content.length()>255){
            commentInfo.setContent(content.substring(0,250));
        }
        commentInfo.setName(account.getName());
        richtextInfoCommentDao.insertSelective(commentInfo);
        return commentInfo;
    }

    /**
     * 删除一条记录
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        richtextInfoCommentDao.deleteByPrimaryKey(id);

    }

    /**
     * 修改一条记录
     *
     * @param commentInfo
     */
    @Override
    public void update(RichtextInfoComment commentInfo) {
        String content = commentInfo.getContent();
        if (content.length()>255){
            commentInfo.setContent(content.substring(0,250));
        }
        richtextInfoCommentDao.updateByPrimaryKeySelective(commentInfo);

    }

    /**
     * 根据主键查询一条记录
     *
     * @param id
     */
    @Override
    public RichtextInfoComment findById(Long id) {
        return richtextInfoCommentDao.selectByPrimaryKey(id);
    }

    /**
     * 查询所有评价信息
     */
    @Override
    public List<RichtextInfoCommentVo> findAll() {
        return richtextInfoCommentDao.findAllVo(null);
    }


    /**
     * 根据条件分页查询
     * @param name
     * @param pageNum  第几页
     * @param pageSize 每页数量
     * @return 当前页的List列表
     */
    @Override
    public PageInfo<RichtextInfoCommentVo> findPage(String name,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<RichtextInfoCommentVo> all = richtextInfoCommentDao.findAllVo(name);
        return PageInfo.of(all);
    }

    /*获取某个车辆的某个评价下级评论信息列表*/
    @Override
    public List<RichtextInfoCommentVo> findByForeignId(Long id) {
        List<RichtextInfoCommentVo> all = richtextInfoCommentDao.findByForeignId(id, 0L);
        for (RichtextInfoCommentVo vo:all){
            Long parentId = vo.getId();
            List<RichtextInfoCommentVo> children = richtextInfoCommentDao.findByForeignId(id, parentId);
            vo.setChildren(children);
        }

        return all;
    }

}
